/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.xo;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

/**
 *
 * @author william
 */
public class XOArray extends XOBase<List<Object>> implements IXOArray {

    public XOArray() {
        m = new ArrayList<Object>();
    }

    public XOArray(Collection collection) {
        if (collection != null) {
            Iterator ite = collection.iterator();
            while (ite.hasNext()) {
                _add(ite.next());
            }
        }
    }

    public XOArray(Object array) {
        if (array.getClass().isArray()) {
            int length = Array.getLength(array);
            for (int i = 0; i < length; i += 1) {
                _add(Array.get(array, i));
            }
        } else {
            throw new RuntimeException("XOList initial value should be collection or array.");
        }
    }

    @Override
    public int _int(int index) {
        return (Integer) m.get(index);
    }

    @Override
    public long _long(int index) {
        return (Long) m.get(index);
    }

    @Override
    public double _double(int index) {
        return (Double) m.get(index);
    }

    @Override
    public Date _date(int index) {
        return (Date) m.get(index);
    }

    @Override
    public IXOObject _xo(int index) {
        return (IXOObject) m.get(index);
    }

    @Override
    public IXOArray _xa(int index) {
        return (IXOArray) m.get(index);
    }

    @Override
    public IXOObject _clone() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int _length() {
        return m.size();
    }

    @Override
    public Object _set(int index, Object value) {
        return m.set(index, value);
    }

    @Override
    public void _add(Object value) {
        m.add(value);
    }

    @Override
    public void _check(int index, String dt, Object value) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int asInt(int index, int dv) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public long asLong(int index, long dv) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public double asDouble(int index, double dv) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Date asDate(int index, Date dv) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public String asString(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public Object obj(int index) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public IXOObject asXO(int index) {
        Object o = m.get(index);
        if (o instanceof IXOObject) {
            return (IXOObject) o;
        }
        return null;
    }

    @Override
    public IXOArray asXA(int index) {
        Object o = m.get(index);
        if (o instanceof IXOArray) {
            return (IXOArray) o;
        }
        return null;
    }

    @Override
    public List<Object> _m() {
        return m;
    }

    @Override
    public String json() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (int i = 0; i < m.size(); i++) {
            sb.append(valueToString(m.get(i))).append(',');
        }
        if (m.size() > 0) {
            sb.setCharAt(sb.length() - 1, ']');
        } else {
            sb.append(']');
        }
        return sb.toString();
    }
    
}
